<?php if( !defined( 'MAIN_DIR' ) ) { header( 'Location: ' . dirname( PHP_SELF ) . '/' ); exit( ); }

	$level = ( isset( $_REQUEST['level'] ) && $_REQUEST['level'] ? $_REQUEST['level'] : -1 );
	$id = ( isset( $_REQUEST['id'] ) && $_REQUEST['id'] ? $_REQUEST['id'] : 0 );
	$props = ( isset( $_REQUEST['props'] ) && $_REQUEST['props'] == "1" ? true : false );
	$recursive = ( isset( $_REQUEST['recursive'] ) && $_REQUEST['recursive'] == "1" ? true : false );
	$random = ( isset( $_REQUEST['random'] ) && $_REQUEST['random'] == "1" ? true : false );
	$limit = ( isset( $_REQUEST['limit'] ) && $_REQUEST['limit'] ? $_REQUEST['limit'] : 0 );
	$propsFilter = ( isset( $_REQUEST['propsFilter'] ) && $_REQUEST['propsFilter'] ? explode( ",", $_REQUEST['propsFilter'] )  : NULL );
	$fileTypes = ( isset( $_REQUEST['fileTypes'] ) && $_REQUEST['fileTypes'] ? $_REQUEST['fileTypes']  : null );
	$_debug = ( isset( $_REQUEST['debug'] ) && $_REQUEST['debug'] ? true  : false );

	$user = null;
	$passwd = null;
	$cacheKey = serialize( $_REQUEST ); 

	if( strlen( SECURE_TYPES ) > 0 )	{
		session_start( );
	
		$user = ( isset( $_SESSION['suser'] ) && $_SESSION['suser'] ? $_SESSION['suser'] : null );
		$passwd = ( isset( $_SESSION['spasswd'] ) && $_SESSION['spasswd'] ? $_SESSION['spasswd'] : null );
		
		if( $user )	{
			if( isset( $_SESSION['sid'] ) && !$id )
				$id = $_SESSION['sid'];
			$cacheKey = serialize( array_merge( $_REQUEST, array( $user, $passwd ) ) );
			unset( $_SESSION['sid'], $_SESSION['suser'], $_SESSION['spasswd'] );
			//session_destroy( );
		}
	}
	
	function showXMLFile ( $file, $limit, $recursive, $level, $random, $fileTypes = null, $props = false, $propsFilter = null, $user = null, $passwd = null )	{
		echo '<file type="' . toXML( $file->getFileType( ) ) . '" name="' . toXML( $file->getName( ) ) . '" id="' . $file->getIdFile( ) . '" >';
		$properties = $file->getProperties( );
		if( strpos( SECURE_TYPES, "'" . $file->getFileType( )  . "'" ) === false || ( $file->getProperty( "user" ) === $user && $file->getProperty( "password" ) === $passwd ) )	{
			if( $props )	{
				foreach( $properties as $key => $value )	{
					if( ( !$propsFilter || in_array( $key, $propsFilter ) ) && $key != 'user' && $key != 'password')
						echo '<' . $key . ' type="' . ( $properties->getType( $key ) == Properties::TYPE_ATTACHMENT ? 'File' : 'Text' ) . '" value="' . ( $properties->getType( $key ) == Properties::TYPE_ATTACHMENT ? toURL( $value ) : str_replace( array( '"', "\r" ), array( "'", '' ), toXML( $value ) ) ) . '" />';
				}
			}
			
			$files = array( );
			if( MIT )
				$files = $file->getChilds( );
			else
				$files = $file->getAllFiles( $fileTypes, $limit, false, $recursive, $random );
			if( !empty( $files ) )	{
				foreach( $files as $lfile )	{
					if( $level != 0 )
						showXMLFile( $lfile, $limit, $recursive, $level - 1, $random, $fileTypes, $props, $propsFilter, $user, $passwd );
					else
						echo '<file type="' . toXML( $lfile->getFileType( ) ) . '" name="' . toXML( $lfile->getName( ) ) . '" id="' . $lfile->getIdFile( ) . '" />';
				}
			}
		}
		else
			echo '<error type="ACCESS" msg="Access Error" />';
		echo '</file>';
	}
	
	if( !$_debug )
		header ( 'content-type: text/xml charset=utf-8' );

	if( !$_debug && cacheExists( $cacheKey, time( ) - CACHE_TIME ) )	{
		cacheRead( $cacheKey );
	}
	else	{
		if( !$_debug )
			echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
		
		$file = new File( );
		if ( $id && $file->load( $id ) )	{
			ob_start( );
			
			if( MIT )
				$file->setChilds( $file->getAllFiles( $fileTypes, $limit, false, $recursive, $random, true ) );
			showXMLFile( $file, $limit, $recursive, $level, $random, $fileTypes, $props, $propsFilter, $user, $passwd );
			
			$content = ob_get_contents( );
			ob_end_flush( );
			if( !$_debug )
				cacheSet( $cacheKey, $content );
			unset( $content );
		}
		else
			echo '<file><error id="404">File not found</error></file>';
	}
